package com.leilei.hibernate;

import com.leilei.MyObject.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.query.Query;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by yvdedu.com on 2016/10/18.
 */
public class hibernate {
    public SessionFactory sessionFactory;

    public hibernate() {
        StandardServiceRegistry build = new StandardServiceRegistryBuilder().configure().build();
        try {
            sessionFactory= new MetadataSources(build).buildMetadata().buildSessionFactory();
        }catch (Exception e){
            StandardServiceRegistryBuilder.destroy(build);
        }
    }


    public <T> T get_UpdateObject(get_sava<T> get_sava,T error){
        try {
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            T t = get_sava.do_save(currentSession);
            currentSession.getTransaction().commit();
            return t;
        }catch (Exception e){
            e.printStackTrace();
            return error;
        }
    }



    public interface get_sava<T>{
        T do_save(Session session);
    }


    public <T> boolean noUse(T t){
        return get_UpdateObject(new get_sava<Boolean>() {
            @Override
            public Boolean do_save(Session session) {
                Class c=t.getClass();
                Method getId = null,setNoUse=null;
                try {
                    getId=c.getMethod("getId");
                    setNoUse=c.getMethod("setNoUse",int.class);
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                    return false;
                }
                try {
                    Object o = session.get(t.getClass(), (int) getId.invoke(t));
                    setNoUse.invoke(o, 1);
                    session.update(o);
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            }
        },false);
    }

    public boolean careUer(int id1,int id2){
        return get_UpdateObject(new get_sava<Boolean>() {
            @Override
            public Boolean do_save(Session session) {
                User user1 = session.get(User.class, id1);
                User user2 = session.get(User.class, id2);
                user1.getCareUsers().add(user2);
                session.save(user1);
                return true;
            }
        },false);
    }

    public boolean noCareUser(int id1,int id2){
        return get_UpdateObject(new get_sava<Boolean>() {
            @Override
            public Boolean do_save(Session session) {
                User user1 = session.get(User.class, id1);
                User user2 = session.get(User.class, id2);
                user1.getCareUsers().remove(user2);
                user2.getRcareUsers().remove(user1);
                session.save(user1);
                session.save(user2);
                return true;
            }
        },false);
    }

    public int getFunsNum(int id){
        return get_UpdateObject(new get_sava<Integer>() {
            @Override
            public Integer do_save(Session session) {
                Query query = session.createQuery("select count(*) from User u left join u.careUsers c where c.id=:id");
                query.setParameter("id",id);
                List list = query.list();
                if (list.size()>0)return Integer.parseInt(String.valueOf(list.get(0)));
                return 0;
            }
        },0);
    }

    public boolean updateUserInfo(UserInfo userInfo){
        return get_UpdateObject(new get_sava<Boolean>() {
            @Override
            public Boolean do_save(Session session) {
                session.update(userInfo);
                return true;
            }
        },false);
    }

    public boolean saveRemark(Remark remark){
        return get_UpdateObject(new get_sava<Boolean>() {
            @Override
            public Boolean do_save(Session session) {
                session.save(remark);
                return true;
            }
        },false);
    }

    public List<Remark> getRemarkData(int id,int page){
        return get_UpdateObject(new get_sava<List<Remark>>() {
            @Override
            public List<Remark> do_save(Session session) {
                Query query = session.createQuery("from Remark where article.id=:id order by createTime desc ");
                query.setParameter("id",id);
                query.setFirstResult(page*30-30);
                query.setMaxResults(30);
                List list = query.list();
                return list;
            }
        },new ArrayList<>());
    }


    public boolean delRemark(int rid,int uid){
        return get_UpdateObject(new get_sava<Boolean>() {
            @Override
            public Boolean do_save(Session session) {
                Query query = session.createQuery("from Remark r where (r.user.id=:uid or r.article.user.id=:uid) and r.id=:rid");
                query.setParameter("uid",uid);
                query.setParameter("rid",rid);
                List list = query.list();
                if (list.size()>0)session.remove(list.get(0));
                return true;
            }
        },false);
    }

    public boolean setPrise(int aid,int uid){
        if (uid==0)return false;
        return get_UpdateObject(new get_sava<Boolean>() {
            @Override
            public Boolean do_save(Session session) {
                Article article = session.get(Article.class, aid);
                User user = session.get(User.class, uid);
                article.getpUsers().add(user);
                user.getpArticles().add(article);
                session.update(article);
                return true;
            }
        },false);
    }

    public boolean delPrise(int aid,int uid){
        return get_UpdateObject(new get_sava<Boolean>() {
            @Override
            public Boolean do_save(Session session) {
                Article article = session.get(Article.class,aid);
                User user = session.get(User.class, uid);
                article.getpUsers().remove(user);
                user.getpArticles().remove(article);
                session.update(article);
                return true;
            }
        },false);
    }

    public boolean getPrise(int aid,int uid){
        if (uid==0)return false;
        return get_UpdateObject(new get_sava<Boolean>() {
            @Override
            public Boolean do_save(Session session) {
                Query query = session.createQuery("from User u inner join u.pArticles a where a.id=:aid and u.id=:uid");
                query.setParameter("aid",aid);
                query.setParameter("uid",uid);
                List list = query.list();
                if (list.size()>0)return true;
                return false;
            }
        },false);
    }

    public int getPriseNum(int aid){
        return get_UpdateObject(new get_sava<Integer>() {
            @Override
            public Integer do_save(Session session) {
                Article article = session.get(Article.class, aid);
                if (article==null)return 0;
                return article.getpUsers().size();
            }
        },0);
    }

    public String get_prise_num(int aid,int uid){
        boolean isPrise=false;
        int priseNum=0;
        if(uid>0&&aid>0){
            isPrise=getPrise(aid,uid);
        }
        if (aid>0)
            priseNum=getPriseNum(aid);
        return "{\"prise\":"+isPrise+",\"num\":\""+priseNum+"\"}";
    }

    public boolean forward(int uid,int aid){
        ArticleInfo articleInfo=new ArticleInfo();
        return get_UpdateObject(new get_sava<Boolean>() {
            @Override
            public Boolean do_save(Session session) {
                Article article = session.get(Article.class,aid);
                User user = session.get(User.class, uid);
                articleInfo.setArticle(article);
                article.setArticleInfo(articleInfo);
                article.getfUsers().add(user);
                user.getfArticles().add(article);
                session.update(article);
                return true;
            }
        },false);
    }
    public boolean getforward(int aid,int uid){
        return get_UpdateObject(new get_sava<Boolean>() {
            @Override
            public Boolean do_save(Session session) {
                Query query = session.createQuery("from User u inner join u.fArticles a where a.id=:aid and u.id=:uid");
                query.setParameter("aid",aid);
                query.setParameter("uid",uid);
                List list = query.list();
                if (list.size()>0)return true;
                return false;
            }
        },false);
    }

    public Article saveArticle(Article article,ArticleInfo articleInfo){
        return get_UpdateObject(new get_sava<Article>() {
            @Override
            public Article do_save(Session session) {
                session.save(article);
                session.save(articleInfo);
                return article;
            }
        },null);
    }


    public Article updateArticle(Article article){
        return get_UpdateObject(new get_sava<Article>() {
            @Override
            public Article do_save(Session session) {
                session.update(article);
                return article;
            }
        },null);
    }

    public List<Article> getSearchArticle(String search,int page){
        return get_UpdateObject(new get_sava<List<Article>>() {
            @Override
            public List<Article> do_save(Session session) {
                Query query = session.createQuery("from Article a where a.content like :search order by createTime desc");
                query.setParameter("search","%"+search+"%");
                query.setFirstResult(page*30-30 );
                query.setMaxResults(30);
                List list = query.list();
                return list;
            }
        },new ArrayList<>());
    }

    public List<Article> getSearchArticle(String search,int uid,int page){
        return get_UpdateObject(new get_sava<List<Article>>() {
            @Override
            public List<Article> do_save(Session session) {
                Query query = session.createQuery("select a from Article a left join a.user.RcareUsers u where a.content like :search and u.id=:id order by a.createTime desc");
                query.setParameter("search","%"+search+"%");
                query.setParameter("id",uid);
                query.setFirstResult(page*30-30 );
                query.setMaxResults(30);
                List list = query.list();
                return list;
            }
        },new ArrayList<>());
    }

    public List<Article> getSearchArticleFromUser(String search,int uid,int page){
        return get_UpdateObject(new get_sava<List<Article>>() {
            @Override
            public List<Article> do_save(Session session) {
                Query query = session.createQuery("from Article a where a.content like :search and a.user.id=:id order by a.createTime desc");
                query.setParameter("search","%"+search+"%");
                query.setParameter("id",uid);
                query.setFirstResult(page*30-30 );
                query.setMaxResults(30);
                List list = query.list();
                return list;
            }
        },new ArrayList<>());
    }

    public boolean changePwd(int id,String pwd){
        return get_UpdateObject(new get_sava<Boolean>() {
            @Override
            public Boolean do_save(Session session) {
                User user = session.get(User.class, id);
                user.setPassWord(pwd);
                session.update(user);
                return true;
            }
        },false);
    }


    public boolean isManager(int id){
        return get_UpdateObject(new get_sava<Boolean>() {
            @Override
            public Boolean do_save(Session session) {
                Query query = session.createQuery("from Manager where user.id=:id");
                query.setParameter("id",id);
                List list = query.list();
                if (list.size()>0)return true;
                return false;
            }
        },false);
    }



    //--------------------------------------------------------------------------------------------------------------------------------


    public List<User> getUsers(){
        try {
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            List<User> list = currentSession.createQuery("from User").list();
            currentSession.getTransaction().commit();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public User getUser(String name){
        try{
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            Query query = currentSession.createQuery("from User where name=:name");
            query.setParameter("name",name);
            List<User> list = query.list();
            User user=null;
            if (list.size()>0)user= list.get(0);
            currentSession.getTransaction().commit();
            return user;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    public User getUser(int id){
        return get_UpdateObject(new get_sava<User>() {
            @Override
            public User do_save(Session session) {
                return session.get(User.class,id);
            }
        },null);
    }


    public UserInfo getUserInfo(int id){
        try{
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            UserInfo userInfo = currentSession.get(UserInfo.class, id);
            currentSession.getTransaction().commit();
            return userInfo;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public boolean saveUser(String name,String passWord){
        User user=new User();
        user.setName(name);
        user.setPassWord(passWord);
        return saveUser(user);
    }

    public boolean saveUser(User user){
        User user1=getUser(user.getName());
        if (user1!=null)return false;
        UserInfo userInfo=new UserInfo();
        userInfo.setUser(user);
        userInfo.setNickName(user.getName());
        userInfo.setPhoto("photo/0.png");
        user.setUserInfo(userInfo);
        Session currentSession = sessionFactory.getCurrentSession();
        try {
            currentSession.beginTransaction();
            currentSession.save(user);
            currentSession.getTransaction().commit();
            return true;
        }catch (Exception e ){
            e.printStackTrace();
            currentSession.close();
            return false;
        }
    }

    public boolean updateUser(User user){
//        User user1=getUser(user.getName());
//        if (user1==null)return saveUser(user);
        Session currentSession = sessionFactory.getCurrentSession();
        try {
            currentSession.beginTransaction();
//            User user2 = currentSession.get(User.class, user.getId());
            currentSession.update(user);
            currentSession.getTransaction().commit();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            currentSession.close();
            return false;
        }
    }


    public boolean toalerPassWord(int uid,String passWord){
        User user=getUser(uid);
        user.setPassWord(passWord);
        try {
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            currentSession.update(user);
            currentSession.getTransaction().commit();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public boolean toalerUserInfo(int id, String nickName, int age, String sex, String intro, int role){
        UserInfo userInfo=getUserInfo(id);
        userInfo.setNickName(nickName);
        userInfo.setAge(age);
        userInfo.setSex(sex);
        userInfo.setIntro(intro);
        userInfo.setRole(role);
        try {
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            currentSession.update(userInfo);
            currentSession.getTransaction().commit();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public boolean saveArticle(int userId,String name,String content,int role,boolean top){
        Article article = new Article();
        article.setUser(getUser(userId));
        article.setName(name);
        article.setContent(content);
        return saveArticle(article,role,top);
    }

    public boolean saveArticle(Article article,int role,boolean top){
        ArticleInfo articleInfo = new ArticleInfo();
        articleInfo.setArticle(article);
        articleInfo.setRole(role);
        if (top){
            articleInfo.setTopTime(new Date());
        }
        try {
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            if (article.getId()>0){
                articleInfo.setId(article.getId());
                currentSession.update(article);
                currentSession.update(articleInfo);
            }else {
                currentSession.save(article);
                currentSession.save(articleInfo);
            }
            currentSession.getTransaction().commit();
            return true;
        }catch (Exception e ){
            e.printStackTrace();
            return false;
        }
    }


    public boolean delArticle(int id){
        Article article = getArticle(id);
        if (article==null)return true;
        try {
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            currentSession.delete(article);
            currentSession.getTransaction().commit();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public List<Article> getRole0FromUser(int uid,int page){
        return get_UpdateObject(new get_sava<List<Article>>() {
            @Override
            public List<Article> do_save(Session session) {
                Query query = session.createQuery("from Article where user.id=:uid and articleInfo.role=0 order by articleInfo.topTime desc,createTime desc");
                query.setParameter("uid",uid);
                query.setFirstResult(page*30-30);
                query.setMaxResults(30);
                return query.list();
            }
        },new ArrayList<>());
    }

    public List<Article> getArticlesFromUser(int uid,int page){
        if (uid<1)return new ArrayList<>();
        return get_UpdateObject(new get_sava<List<Article>>() {
            @Override
            public List<Article> do_save(Session session) {
                Query query = session.createQuery("from Article where user.id=:uid order by articleInfo.topTime desc,createTime desc");
                query.setParameter("uid",uid);
                query.setFirstResult(page*30-30);
                query.setMaxResults(30);
                return query.list();
            }
        },new ArrayList<>());
    }


    public List<Article> getArticlesFromUser(int id1,int id2,int page){
        if (id2<1)return new ArrayList<>();
        if (id1<1) return getRole0FromUser(id2,page);
        if (id1==id2) return getArticlesFromUser(id1,page);
        return get_UpdateObject(new get_sava<List<Article>>() {
            @Override
            public List<Article> do_save(Session session) {
                String hql1="select a from Article a " +
                        "left join a.articleInfo ai " +
                        "left join a.user au " +
                        "left join au.blackUser aub " +
                        "left join au.careUsers auc " +
                        "left join au.RcareUsers aurc " +
                        "where " +
                        "(au.id=:user2) and (" +
                        "(ai.role=0) or " +
                        "(ai.role=1 and aub is empty) or " +
                        "(ai.role=1 and aub.id<>:user_0) or " +
                        "(ai.role=2 and aurc.id=:user_1) or " +
                        "(ai.role=3 and aurc.id=:user_2 and auc.id=:user_3) " +
                        ") group by a.id " +
                        "order by ai.topTime desc,a.createTime desc";
                Query query = session.createQuery(hql1);
                query.setParameter("user2",id2);
                query.setParameter("user_0",id1);
                query.setParameter("user_1",id1);
                query.setParameter("user_2",id1);
                query.setParameter("user_3",id1);
                query.setFirstResult(page*30-30);
                query.setMaxResults(30);
                return query.list();
            }
        },new ArrayList<>());

    }


    public List<Article> getArticlesFromCare(int uid,int page){
        if (uid<1) return new ArrayList<>();
        try{
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            Query query = currentSession.createQuery("select a from Article a " +
                    "left join a.articleInfo ai " +
                    "left join a.user au " +
                    "left join au.blackUser aub " +
                    "left join au.careUsers auc " +
                    "left join au.RcareUsers aurc " +
                    "where ("+
                        "(au.id=:u) or " +
                        "(aurc.id=:u1) and (" +
                            "(ai.role=0) or " +
                            "(ai.role=1 and aub is empty) or " +
                            "(ai.role=1 and aub.id<>:u2) or " +
                            "(ai.role=2 and aurc.id=:u3) or " +
                            "(ai.role=3 and aurc.id=:u4 and auc.id=:u5) " +
                        ")" +
                    ") group by a.id " +
                    "order by a.createTime desc ");
            query.setParameter("u",uid);
            query.setParameter("u1",uid);
            query.setParameter("u2",uid);
            query.setParameter("u3",uid);
            query.setParameter("u4",uid);
            query.setParameter("u5",uid);
            query.setFirstResult(page*30-30);
            query.setMaxResults(30);
            List<Article> list = query.list();
            currentSession.getTransaction().commit();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public List<Article> getArticleAtTodayUp(int uid,int page){
        if (uid<1)return getRole0(page);
        try{
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            Query query = currentSession.createQuery("select a from Article a " +
                    "left join a.articleInfo ai " +
                    "left join a.user au " +
                    "left join au.blackUser aub " +
                    "left join au.careUsers auc " +
                    "left join au.RcareUsers aurc " +
                    "where (" +
                    "(au.id=:u) or " +
                    "(ai.role=0) or " +
                    "(ai.role=1 and aub is empty) or " +
                    "(ai.role=1 and aub.id<>:u1) or " +
                    "(ai.role=2 and aurc.id=:u2) or " +
                    "(ai.role=3 and aurc.id=:u3 and auc.id=:u4)) " +
                    "group by a.id " +
                    "order by a.createTime desc ");
//            Query query = currentSession.createQuery("select a from Article a left join ArticleInfo aa on a.id=aa.id where aa.role=:role order by a.fUsers.size+a.pUsers.size desc ");
            //NativeQuery query = currentSession.createSQLQuery("select a.* from article a left join (select article_id,count(*) as c from fabulous group by article_id) b on a.id=b.article_id  order by c desc");
            query.setParameter("u",uid);
            query.setParameter("u1",uid);
            query.setParameter("u2",uid);
            query.setParameter("u3",uid);
            query.setParameter("u4",uid);
            query.setFirstResult(page*30-30);
            query.setMaxResults(30);
            List<Article> list = query.list();
            currentSession.getTransaction().commit();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public List<Article> getRole0(int page){
        return get_UpdateObject(new get_sava<List<Article>>() {
            @Override
            public List<Article> do_save(Session session) {
                Query query = session.createQuery("from Article where articleInfo.role=0 order by createTime desc ");
                query.setFirstResult(page*30-30);
                query.setMaxResults(30);
                return query.list();
            }
        },new ArrayList<>());
    }



    public Article getArticle(int id){
        try{
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            Article article = currentSession.get(Article.class, id);
            currentSession.getTransaction().commit();
            return article;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    public List<Article> getArticles(int userId,int page){
        try {
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            Query query = currentSession.createQuery("from Article where user.id=:userId");
            query.setParameter("userId",userId);
            query.setFirstResult(page*30-30);
            query.setMaxResults(30);
            List<Article> articles=query.list();
            currentSession.getTransaction().commit();
            return articles;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public  ArticleInfo getArticleInfo(int id){
        try {
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            ArticleInfo articleInfo = currentSession.get(ArticleInfo.class, id);
            currentSession.getTransaction().commit();
            return articleInfo;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public List<User> getFriends(int uid){
        return get_UpdateObject(new get_sava<List<User>>() {
            @Override
            public List<User> do_save(Session session) {
                Query query = session.createQuery("select u from User u left join u.careUsers c left join u.RcareUsers b where c.id=:user and b.id=:user and u.id!=:user");
                query.setParameter("user",uid);
                return query.list();
            }
        },new ArrayList<>());
    }

    public boolean caring(int uid1,int uid2){
        if (uid1<1||uid2<1)return false;
        try {
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            Query query = currentSession.createQuery("select u from User u inner join u.careUsers c where u.id=:user1 and c.id=:user2");
            query.setParameter("user1",uid1);
            query.setParameter("user2",uid2);
            List<User> list = query.list();
            currentSession.getTransaction().commit();
            if (list.size()>0)return true;
            return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public boolean nasty(int uid1,int uid2){
        if (uid1<1||uid2<1)return false;
        try {
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            Query query = currentSession.createQuery("select u from User u inner join u.blackUser b where u.id=:user1 and b.id=:user2");
            query.setParameter("user1",uid1);
            query.setParameter("user2",uid2);
            List list = query.list();
            currentSession.getTransaction().commit();
            if (list.size()>0)return true;
            return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }


    public int getRole(int uid1,int uid2){
        if (uid1<1||uid2<1)return 0;
        if (nasty(uid2,uid1))return 1;
        if (caring(uid2,uid1)){
            if (caring(uid1,uid2))
            return 3;
            return 2;
        }
        if (uid1==uid2)return 4;
        return 0;
    }



    public boolean toalerArticle(int id,String name,String content){
        Article article=getArticle(id);
        article.setName(name);
        article.setContent(content);
        try {
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            currentSession.update(article);
            currentSession.getTransaction().commit();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public boolean toalerArticleInfo(int id,int role,boolean top){
        ArticleInfo articleInfo=getArticleInfo(id);
        articleInfo.setRole(role);
        if (top){
            articleInfo.setTopTime(new Date());
        }
        try {
            Session currentSession = sessionFactory.getCurrentSession();
            currentSession.beginTransaction();
            currentSession.update(articleInfo);
            currentSession.getTransaction().commit();
            return true;
        }catch (Exception e ){
            e.printStackTrace();
            return false;
        }
    }




    private Date Time(int i){
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String format = df.format(new Date());
//        try {
//            Date date=df.parse(format);
//            return date;
//        } catch (ParseException e) {
//            e.printStackTrace();
//            return new Date();
//        }
        Calendar cal = Calendar.getInstance();
        Date date = new Date();
        cal.setTime(date);
        cal.add(Calendar.DATE,i);
        return cal.getTime();
    }


}
